﻿@using KPIS.GERP.Models.ICM
@model IEnumerable<Bidder>

@{
    ViewBag.Title = "คัดเลือกผู้ชนะ";
    Layout = "~/Views/Shared/_EmptyLayout.cshtml";

    var BidService = new KPIS.GERP.WEB.GIMS.BidService.BidServiceClient();
}
<div class="row-fluid">
    <div class="table-header span12">
        <div class="span10">
			@ViewBag.Title
        </div>
    </div>
</div>
<div class="block-button-menu-above" style="padding-top:10px">
    <a href='@Url.Action("SendBidResult", new { id = ViewBag.BidId})' class="btn btn-small button-standard-width btn-warning" data-dismiss="modal" role="button">คัดเลือก</a>
    @*<a href="/BidderSelect/SendBidResult/@ViewBag.BidId" class="btn btn-small button-standard-width btn-warning" data-dismiss="modal" role="button">ยืนยันผลการคัดเลือก</a>*@
    <a href="#" class="btn btn-small button-standard-width btn-success" onclick="window.location.reload();" role="button">โหลดข้อมูลใหม่</a>
    @*@Html.ButtonSubmit()*@
</div>
<div class="row-fluid">
<table id="table" class="table">
    <thead>
        <tr>
            <th>
                สินค้า/บริการ
            </th>
            @foreach (var m in Model ?? new List<Bidder>())
            {
                <th>@m.MasContact.NameTh</th>
            }
        </tr>
    </thead>
    <tbody>
        @foreach (var m in (IEnumerable<PurchaseRequestItem>)ViewBag.ItemModel ?? new List<PurchaseRequestItem>())
        {
            <tr style="vertical-align:top">
                <td>
                    <label class="green">
                        @(m.ResItemId.HasValue ? m.MasResourceItem.Name : m.Descr != null || m.Descr.Length > 0 ? m.Descr : "ไม่ระบุพัสดุที่ซื้อ/จ้างทำ")
                    </label>                   
                    จำนวน @((m.Qty ?? 0).ToString("#,##0.##")) 
                    @(m.MasResourceItem != null && m.MasResourceItem.MasUnit != null ? m.MasResourceItem.MasUnit.Name : "")
                </td>
                @foreach (var n in Model ?? new List<Bidder>())
                {
                    var QuotationModel = BidService.SelectBidderQuotation(new BidderQuotation() { PrItemId = m.PrItemId, BidderId = n.BidderId }, null).FirstOrDefault();
                    
                    if (QuotationModel != null)
                    {
                        bool bSelect = QuotationModel.Selected == "1";
                        <td @(bSelect ? "class=btn-danger" : "") >
                            
                            <label>
                                <input id="@QuotationModel.BidderQuoId" name="selected" type="checkbox" 
                                       class="ace" @(bSelect ? "checked" : "")>
                                <span class="lbl"></span>
                            </label>

                            <div class="control-group">
                                <div class="control-label">
                                    ราคาสุทธิ
                                </div>
                                <div class="controls-detail" style="margin-top:5px !important;">
                                    <label @(bSelect ? "class=white" : "class=blue")>@(QuotationModel.NetAmount.HasValue ? QuotationModel.NetAmount.Value.ToString("#,##0.00") : "0.00") บาท</label>
                                </div>
                            </div>

                            <div class="control-group">
                                <div class="control-label">
                                    รายละเอียดเพิ่มเติม
                                </div>
                                <div class="controls-detail" style="margin-top:5px !important;">
                                    <label @(bSelect ? "class=white" : "class=blue")>@QuotationModel.Descr</label>
                                </div>
                            </div>

                        </td>
                    }
                    else
                    {
                        <td></td>
                    }
                }
            </tr>
        }
    </tbody>
</table>
</div>

<div id="modal" class="modal hide fade autosize" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="height:400px"></div>

@section scripts
{
    <script>
        $(function () {
            $("[type='checkbox']").change(function () {
                var chk = this;
                var $modal = $('#modal');
                var id = chk.id;
                var url = "";

                if (chk.checked) {
                    url = '@Url.Action("Select")' + '/' + id;

                    $modal.html('<iframe width="100%" height="100%" frameborder="0" scrolling="no" allowtransparency="true" src="' + url + '"></iframe>');
                    $modal.modal({ show: true });
                }
                else {
                    $.ajax({
                        url: '@Url.Action("Deselect", "BidderSelect")' + id,
                        success: function (result) {
                            chk.closest("ul").css("background-color", "white");
                        }
                    });
                }
            });
        });
    </script>
}